package com.xf.sys.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Service;

import com.xf.sys.entity.SysResource;
import com.xf.sys.entity.SysResourcesRole;
import com.xf.sys.mapper.SysResourcesRoleMapper;
import com.xf.sys.service.ISysResourcesRoleService;

/**
 * SysResourcesRoleService.java
 * @author lb
 * 2017年5月27日
*/
@Service
public class SysResourcesRoleService implements ISysResourcesRoleService {
	
	private final Logger logger =  LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private SysResourcesRoleMapper sysResourcesRoleMapper;
	
	@Autowired
	private RedisTemplate<String, String> redisTemplate;

	@Override
	public List<SysResource> selectSysResourceRoleList(Map<String, Integer> map) {
		List<SysResource> list=null;
		try {
			list=sysResourcesRoleMapper.selectSysResourceRoleList(map);
			if(null==list){
				list=new ArrayList<SysResource>();
			}
		} catch (Exception e) {
			logger.error("selectSysResourceRoleList");
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public int updateSysResourceRole(Integer[] resourceId, Integer roleId) {
		int result=0;
		sysResourcesRoleMapper.deleteByRoleId(roleId);
		SysResourcesRole sysResourcesRole=null;
		List<SysResourcesRole> list=new ArrayList<SysResourcesRole>();
		for(int i=0;i<resourceId.length;i++){
			RedisAtomicLong reroId = new RedisAtomicLong("reroId", redisTemplate.getConnectionFactory());
			sysResourcesRole=new SysResourcesRole();
			sysResourcesRole.setId((int)reroId.incrementAndGet());
			sysResourcesRole.setResourceId(resourceId[i]);
			sysResourcesRole.setRoleId(roleId);
			list.add(sysResourcesRole);
		}
		result=sysResourcesRoleMapper.insertBatch(list);
		return result;
	}

}
